package com.yzg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.pojo.Role;
import com.yzg.service.RoleService;
import com.yzg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: decade
 * @Description: 角色管理
 * @date: 2021/1/14 23:48
 */
@Api(tags = "RoleController", description = "角色管理")
@RestController
@RequestMapping("/acl/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "管理角色分页列表")
    @GetMapping("/{page}/{pageSize}")
    public R getRoleList(@ApiParam(name = "page", value = "当前页码", required = true)
                             @PathVariable Long page,
                         @ApiParam(name = "pageSize", value = "当前页大小", required = true)
                             @PathVariable Long pageSize,
                         @ApiParam(name = "roleQueryVo", value = "角色查询名称", required = true)
                                     Role roleQueryVo
                         ){
        IPage<Role> pageParam = new Page<>(page,pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(roleQueryVo.getRoleName())){
            wrapper.like("role_name", roleQueryVo.getRoleName());
        }
        roleService.page(pageParam, wrapper);
        return R.ok().data("roleList", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @ApiOperation(value = "添加管理角色")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "role", value = "角色信息", required = true)
            @RequestBody Role role
    ){
        roleService.save(role);
        return R.ok();
    }

    @ApiOperation(value = "根据管理角色id获取角色信息")
    @GetMapping("/getRole/{id}")
    public R getById(
            @ApiParam(name = "id", value = "角色id", required = true)
            @PathVariable String id
    ){
        Role role = roleService.getById(id);
        return R.ok().data("role",role);
    }

    @ApiOperation(value = "修改管理角色")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "role", value = "角色信息", required = true)
            @RequestBody Role role
    ){
        roleService.updateById(role);
        return R.ok();
    }

    @ApiOperation(value = "删除管理角色")
    @DeleteMapping("/remove/{id}")
    public R remove(
            @ApiParam(name = "id", value = "角色id", required = true)
            @PathVariable String id
    ){
        roleService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "批量删除管理角色")
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList", value = "角色id集合", required = true)
            @RequestBody List<String> idList
    ){
        roleService.removeByIds(idList);
        return R.ok();
    }
}
